/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ie.tcd.cs.railtickets.server;

import ie.tcd.cs.railtickets.commons.filter.JarFileFilter;
import ie.tcd.cs.railtickets.commons.util.ClassLoaderUtil;
import ie.tcd.cs.railtickets.commons.util.PropertiesUtil;
import java.io.File;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ie.tcd.cs.railtickets.server.dao.impl.HibernateSessionFactory;

/**
 *
 * @author Wenbo Sun
 */
public class Startup {

    private static final Log log = LogFactory.getLog(Startup.class);

    private static String LIBFOLDER = "lib";
    private static String HIBERNATE = "config/hibernate.properties";

    public void startUp() {
        try {
            log.debug("system startup ...");
            this.loadLib(LIBFOLDER);
            HibernateSessionFactory.addProperties(PropertiesUtil.getProperties(HIBERNATE));
        } catch (IOException ex) {
            log.error("startup failed!", ex);
        }
    }


    public void loadLib(String lib) {
        log.info("loading lib ...");
        try {
            File file = new File(lib);
            for (File jar : file.listFiles(new JarFileFilter())) {
                ClassLoaderUtil.addFile(jar);
            }
        } catch (IOException ex) {
            log.error("load lib failed !", ex);
        }
    }
}
